package 码云作业;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class text04 {
	 public static void writeObject(String path) {
	        ObjectOutputStream oos = null;
	        Stack stf1 = new Stack("张三", 21, 4500);
	        Staff stf2 = new Staff("李四", 24, 5500);
	        try {
	            oos = new ObjectOutputStream(new FileOutputStream(path));
	            oos.writeObject(stf1);
	            oos.writeObject(stf2);
	            oos.writeObject(null);
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (oos != null) {
	                try {
	                    oos.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	    }

	    public static void readObject(String path) {
	        ObjectInputStream ois = null;
	        try {
	            ois = new ObjectInputStream(new FileInputStream(path));
	            Object obj = ois.readObject();
	            while (obj != null) {
	                Stack staff = (Stack) obj;
	                System.out.println(staff.toString());
	                obj = ois.readObject();
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        } catch (ClassNotFoundException e) {
	            e.printStackTrace();
	        } finally {
	            if (ois != null) {
	                try {
	                    ois.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	    }

	    public static void saveStaffToFile(Stack staff, File file) {
	        String str = ((File) staff).getName() + "/" + ((Object) staff).getAge() + "/" + ((Object) staff).getSalary() + "\n";
	        FileOutputStream fos = null;
	        try {
	            fos = new FileOutputStream(file, true);
	            fos.write(str.getBytes());
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (fos != null) {
	                try {
	                    fos.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	    }

	    public static List<Stack> readStaffFromFile(File file) {
	        List<Stack> staffs = new ArrayList<Stack>();
	        BufferedReader br = null;
	        try {
	            br = new BufferedReader(new FileReader(file));
	            String str = br.readLine();
	            while (str != null) {
	                String[] strs = str.split("/");
	                staffs.add(new Stack());
	                str=br.readLine();
	            }
	        } catch (IOException e) {
	            e.printStackTrace();
	        } finally {
	            if (br != null) {
	                try {
	                    br.close();
	                } catch (IOException e) {
	                    e.printStackTrace();
	                }
	            }
	        }
	        return staffs;
	    }

	    public static void main(String[] args) {
	        writeObject("doc\\staff.bin");
	        readObject("doc\\staff.bin");

	        Stack staff = new Stack();
	        File file = new File("myDoc\\test.txt");
	        saveStaffToFile(staff, file);

	        List<Stack> staffs = readStaffFromFile(file);
	        for (Stack stf:staffs){
	            System.out.println(stf.toString());
	        }
	    }
}
